﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace MWTWorkUnitMgmtTool.DataAccess.TableGatewayRepository
{
    public class SpecialWorkunitRepository:ISpecialWorkunitRepository
    {
        string _connectionString;
        private ITableGatewayRepository _tableGatewayRepository; 
        public SpecialWorkunitRepository(ITableGatewayRepository tableGatewayRepository)
        {
            _connectionString = ConfigurationManager.AppSettings["DB_SAPB1Gateway"].ToString();
            _tableGatewayRepository = tableGatewayRepository; 
        }
        #region ISpecialWorkunitRepository Members

        public IEnumerable<IDataRecord> GetAllSpecialWorkunits()
        {
            return _tableGatewayRepository.Retrieve("SELECT Code, Description,Type, Facility FROM tblSpecialWorkUnits", _connectionString, delegate(SqlParameterCollection p)
            {
            }); 
        }

        public IEnumerable<IDataRecord> GetSpecialWorkunitByCode(string workUnitCode)
        {
            return _tableGatewayRepository.Retrieve("SELECT Code, Description,Type, Facility FROM tblSpecialWorkUnits WHERE Code=@WUCode", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", SqlDbType.VarChar).Value = workUnitCode;
            });
        }

        public int InsertSpecialWorkunit(string wuCode, string wuName, string type, string facility)
        {
            return _tableGatewayRepository.Commit("INSERT INTO tblSpecialWorkunits VALUES(@WUCode, @WUName, @Type, @Facility)", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", SqlDbType.VarChar).Value = wuCode;
                p.Add("@WUName", SqlDbType.VarChar).Value = wuName;
                p.Add("@Type", SqlDbType.VarChar).Value = type;
                p.Add("@Facility", SqlDbType.VarChar).Value = facility;
            });
        }

        public int UpdateSpecialWorkunit(string wuCode, string wuName, string type, string facility)
        {
            return _tableGatewayRepository.Commit("UPDATE tblSpecialWorkunits SET Description=@WUName, Type=@Type, Facility=@Facility WHERE Code=@WUCode", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", SqlDbType.VarChar).Value = wuCode;
                p.Add("@WUName", SqlDbType.VarChar).Value = wuName;
                p.Add("@Type", SqlDbType.VarChar).Value = type;
                p.Add("@Facility", SqlDbType.VarChar).Value = facility; 
            });
        }

        public int DeleteSpecialWorkunit(string wuCode)
        {
            return _tableGatewayRepository.Commit("DELETE FROM tblSpecialWorkunits WHERE Code=@WUCode", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", SqlDbType.VarChar).Value = wuCode; 
            });
        }

        #endregion



    }
}
